import Phaser from 'phaser'
import Configer from '../types/configer';
import PlayerBase from '../players/PlayerBase'
import Director from '../players/Director';
import NetClient from '../types/NetClient';


export default class Game1V1 extends Phaser.Scene {
    public director!: Director;
    private clientNt: NetClient = null!;
    private isGameOver: boolean = false;
    public rxCount: number = 0;
    private tian!: Phaser.GameObjects.Container;

    constructor() {
        super('Game1V1');
    }

    /** 创建角色控制需要的按键 */
    keyboardInit() {
    }

    send(action: string, data: any = null) {
        if (this.clientNt.stateConnect == true) {
            this.clientNt.sendAction('Moba2D', action, data);
        }
    }

    gameInit() {
        this.clientNt = window.client;

        if (this.clientNt != null && this.clientNt.stateConnect) {
            this.clientNt.currentObject = this;
            this.send('setLocationMapArea', { 'locationMapArea': 1 });
            setInterval(() => {
                this.director.rxCount = this.rxCount;
                this.rxCount = 0;
            }, 1000);
        }
    }

    onNetworkRecv(status: boolean, action: string = '', data: any = null) {
        this.rxCount++;

        if (status == false) {
            console.log('网络异常');
            return;
        }
        if (action == "attackScope") {
            this.director.playerMain.scope(data['sx'], data['sy'], data['ex'], data['ey']);
        }

        if (action == 'beHit') {
            console.log(data);
            if (data['robot'] == true) {
                let player = <PlayerBase>this.director.monsterMapping[data['id']];
                player.beHit(data['physicsHurt'], data['magicHurt'], data['repelValue'], data['shihuaTime'])
            } else {
                let player = <PlayerBase>this.director.playerMapping[data['id']];
                player.beHit(data['physicsHurt'], data['magicHurt'], data['repelValue'], data['shihuaTime'])
            }
        }

        if (action == 'crit') {
            if (data['robot'] == true) {
                let player = <PlayerBase>this.director.monsterMapping[data['id']];
                player.crit();
            } else {
                let player = <PlayerBase>this.director.playerMapping[data['id']];
                player.crit();
            }
        }

        if (action == 'missed') {
            if (data['robot'] == true) {
                let player = <PlayerBase>this.director.monsterMapping[data['id']];
                player.missed();
            } else {
                let player = <PlayerBase>this.director.playerMapping[data['id']];
                player.missed();
            }
        }

        if (action == 'syncState') {
            this.director.update(data);
        }
        if (action == "gameOver") {
            if (this.isGameOver == true) return;
            this.scene.stop('Dashboard');
            setTimeout(() => {
                this.scene.start('GameOver', { data: data })
            }, 5000);
            this.isGameOver = true;
        }

        if (action == "removeMonster") {
            console.log('removeMonster')
            let player = <PlayerBase>this.director.monsterMapping[data['id']];
            // this.director.monsterList.indexOf(player);
            this.director.monsterList.splice(this.director.monsterList.indexOf(player), 1);
            if (player.spine) {
                player.spine.setActive(false)
                player.spine.setVisible(false);
                player.spine.destroy();
                player.statusUi.destroy();
            }
            delete this.director.monsterMapping[data['id']]
            this.director.monsterMapping[data['id']] = null;
        }
    }

    initMap() {
        this.tian = this.add.container(200, 30, [
            this.add.image(1000, 0, 'once_tian').setScale(0.5, 0.5),
            this.add.image(500, 0, 'once_tian').setScale(0.5, 0.5),
            this.add.image(0, 0, 'once_tian').setScale(0.5, 0.5),
        ])

        this.tian.setScrollFactor(0);

        // let iro1= this.add.image(0, 220, 'one_ironfence').setScale(0.5, 0.5);
        // let iro2= this.add.image(1020, 220, 'one_ironfence').setScale(0.5, 0.5);
        // let iro3= this.add.image(2040, 220, 'one_ironfence').setScale(0.5, 0.5);

        // iro1.setDepth(9999);
        // iro2.setDepth(9999);
        // iro3.setDepth(9999);

        this.add.container(510, 350, [
            this.add.image(0, -200, 'one_grass').setScale(0.5, 0.5),
            this.add.image(1020, -200, 'one_grass').setScale(0.5, 0.5),
            this.add.image(2040, -200, 'one_grass').setScale(0.5, 0.5),
            this.add.image(0, 0, 'mapmain').setScale(0.5, 0.5),
            this.add.image(1020, 0, 'mapmain').setScale(0.5, 0.5),
            this.add.image(2040, 0, 'mapmain').setScale(0.5, 0.5),

            this.add.image(-400, -200, 'once_enclosure3').setScale(0.5, 0.5),
            this.add.image(-220, -190, 'once_enclosure2').setScale(0.5, 0.5),
            this.add.image(0, -190, 'once_enclosure4').setScale(0.5, 0.5),
            this.add.image(265, -191, 'once_enclosure1').setScale(0.5, 0.5),
            this.add.image(570, -190, 'once_enclosure4').setScale(0.5, 0.5),

            this.add.image(1900, -200, 'once_enclosure3').setScale(0.5, 0.5),
            this.add.image(2100, -190, 'once_enclosure2').setScale(0.5, 0.5),
            this.add.image(2305, -190, 'once_enclosure4').setScale(0.5, 0.5),
            this.add.image(2570, -191, 'once_enclosure1').setScale(0.5, 0.5),

            this.add.image(1500, -200, 'one_lousycar').setScale(0.5, 0.5),
        ]);

    }


    create() {
        this.isGameOver = false;
        this.cameras.main.setBounds(0, 0, Configer.GAME_MAP_WIDTH, Configer.GAME_MAP_HEIGHT);
        // this.cameras.main.
        console.log(this.cameras.main.width, this.cameras.main.height)

        // 初始化地图
        this.initMap();

        this.director = new Director(this);
        /** 创建测试数据 */
        // this.director.createTest();
        // this.director.switchPlayer();

        this.scene.launch('Dashboard', { scene: this });
        /** 添加控制按键 */
        this.keyboardInit();
        this.gameInit();
    }

    update() {

        /** 动态更新玩家渲染层级 */
        for (let i = 0; i < this.director.playerList.length; i++) {
            this.director.playerList[i].spine.depth = this.director.playerList[i].spine.y;
            this.director.playerList[i].update();
        }

        for (let i = 0; i < this.director.monsterList.length; i++) {
            this.director.monsterList[i].spine.depth = this.director.monsterList[i].spine.y;
            this.director.monsterList[i].update();
        }

    }
}
